package org.example.controller;


import org.example.internalCommon.constant.CommonStatusEnum;
import org.example.internalCommon.dto.DriverCarBindingRelationship;
import org.example.internalCommon.dto.ResponseResult;
import org.example.service.DriverCarBindingRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 司机车辆关系表 前端控制器
 * </p>
 *
 * @author hh
 * @since 2024-12-31
 */
@RestController
@RequestMapping("/driver-car-binding-relationship")
public class DriverCarBindingRelationshipController {

    @Autowired
    private DriverCarBindingRelationshipService driverCarBindingRelationshipService;


    @PostMapping("/bind")
    public ResponseResult bind(@RequestBody DriverCarBindingRelationship driverCarBindingRelationship) {
        // 如果参数中的车辆和司机已经做过绑定了，则不允许再次绑定
        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipService.getDriverCarBindingRelationships(driverCarBindingRelationship);
        if (driverCarBindingRelationships!=null && driverCarBindingRelationships.size() > 0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
        }
        return driverCarBindingRelationshipService.bind(driverCarBindingRelationship);
    }


    @PostMapping("/unbind")
    public ResponseResult unbind(@RequestBody DriverCarBindingRelationship driverCarBindingRelationship){
        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingRelationshipService.getDriverCarBindingRelationships(driverCarBindingRelationship);
        if (driverCarBindingRelationships.isEmpty()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue());
        }

        DriverCarBindingRelationship relationship = driverCarBindingRelationships.get(0);


        return driverCarBindingRelationshipService.unbind(relationship);

    }

}
